'use strict';

const Controller = require('egg').Controller;
const moment = require('moment')

class Fitment extends Controller {
  async historyList() {
    const {
      ctx,
      app
    } = this;
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    try {
      let fitment = await ctx.model.Fitment.findAndCountAll({
        where: {
          del: 1
        },
        order: [
          ['created_at', 'DESC'],
        ],
        include: {
          model: ctx.model.User,
          attributes: ['username'],
        },
        limit: pageshow,
        offset: (page - 1) * pageshow
      })
      ctx.body = {
        code: 200,
        data: {
          list: fitment.rows.map(item => {
            let obj = JSON.parse(JSON.stringify(item))
            return {
              ...obj,
              created_at: moment(obj.created_at * 1000).format('YYYY-MM-DD HH:mm:ss')
            }
          }),
          count: fitment.count
        },
        msg: '操作成功'
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        type: 'FITMENT_ERROR',
        msg: '获取列表失败'
      }
    }
  }
  async getFitment() {
    const {
      ctx
    } = this
    let type = ctx.request.body.type
    let zx_id = ctx.request.body.zx_id
    let user_id = ctx.request.body.user_id
    try {
      if (type == 'huifu') {
        let fitment = await ctx.model.Fitment.findOne({
          where: {
            zx_id
          }
        })
        await fitment.set({
          user_id,
          created_at: Date.parse(new Date()) / 1000
        }).save().then(res => {
          return ctx.body = {
            code: 200,
            data: res,
            msg: '恢复成功'
          }
        }).catch(err => {
          return ctx.body = {
            code: 0,
            msg: '恢复失败'
          }
        })
      } else if (type == 'del') {
        let fitment = await ctx.model.Fitment.findOne({
          where: {
            zx_id
          }
        })
        await fitment.set({
          user_id,
          del: 0,
          created_at: Date.parse(new Date()) / 1000
        }).save().then(res => {
          return ctx.body = {
            code: 200,
            data: res,
            msg: '删除成功'
          }
        }).catch(err => {
          return ctx.body = {
            code: 0,
            msg: '删除失败'
          }
        })
      } else {
        let fitment = await ctx.model.Fitment.findAll({
          where: {
            del: 1
          },
          order: [
            ['created_at', 'DESC'],
          ],
          attributes: ['text', 'zx_id'],
          limit: 1
        })
        return ctx.body = {
          code: 200,
          data: {
            fitment
          },
          msg: '操作成功'
        }
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        type: 'GET_FITMENT_ERROR',
        msg: '获取失败'
      }
    }
  }
  async setFitment() {
    const {
      ctx
    } = this
    let text = ctx.request.body.text
    let user_id = ctx.request.body.user_id
    let data = await ctx.model.Fitment.create({
      user_id,
      text,
      del: 1,
      created_at: Date.parse(new Date()) / 1000
    })
    ctx.body = {
      code: 200,
      data,
      msg: '操作成功'
    }
  }
}

module.exports = Fitment;